//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<CSimpleMesh9.cpp>
///	@path	~/src/graphics/dx9/
///	@date	2009/01/10
///	@desc	.

#include "config/config.h"

#include "graphics/dx9/CSimpleMesh9.h"

namespace xeres {

	IMPL_IOBJECT_CLASS( CSimpleMesh9 );
	
	// CSimpleMesh9
	CSimpleMesh9::CSimpleMesh9( TopologyType type , RefWeak<IVertexLayout> layout , RefWeak<IVertexBuffer> vertex , RefWeak<IVertexIndex> index )
		: m_type( d3d_topology_type9( type ) )
		, m_layout( layout )
		, m_vertex( vertex )
		, m_index( index )
	{
		size_t count = 0;
		if( m_index.IsValid() )
			count = m_index->GetCount();
		m_prim = d3d_prim_count( type , vertex->GetCount() , count );
	}
	// ~CSimpleMesh9
	CSimpleMesh9::~CSimpleMesh9( void )
	{
	}
	// OnLostDevice
	bool CSimpleMesh9::OnLostDevice( void )
	{
		return true;
	}
	// OnResetDevice
	bool CSimpleMesh9::OnResetDevice( RefWeak<IDevice> device )
	{
		return true;
	}
	// GetLayout
	RefWeak<IVertexLayout> CSimpleMesh9::GetLayout( void )
	{
		return m_layout;
	}
	// EstablishBuffer
	void CSimpleMesh9::EstablishBuffer( RenderContext& rc )
	{
		m_layout->ApplyLayout( rc );
		m_vertex->ApplyStream( rc , 0 );
		if( m_index.IsValid() )
			m_index->ApplyIndex( rc );
	}
	// Draw
	void CSimpleMesh9::Draw( RenderContext& rc )
	{
		if( m_index.IsValid() )
		{
			rc.GetDX9()->DrawIndexedPrimitive( m_type , 0 , 0 , m_vertex->GetCount() , 0 , m_prim );
		}
		else
		{
			rc.GetDX9()->DrawPrimitive( m_type , 0 , m_prim );
		}
	}
	// ClearBuffer
	void CSimpleMesh9::ClearBuffer( RenderContext& rc )
	{
		rc.GetDX9()->SetStreamSource( 0 , NULL , 0 , 0 );
		if( m_index.IsValid() )
			rc.GetDX9()->SetIndices( NULL );
	}
	// GetSlotCount
	size_t CSimpleMesh9::GetSlotCount( void )
	{
		return 0;
	}
	// GetVertex
	RefWeak<IVertexBuffer> CSimpleMesh9::GetVertex( size_t slot )
	{
		XS_ASSERT( slot == 0 );
		return m_vertex;
	}
	// GetIndex
	RefWeak<IVertexIndex> CSimpleMesh9::GetIndex( void )
	{
		return m_index;
	}
	
} // namespace xeres
